//配置axios的全局基本路径，访问后端接口使用8080
axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios

// 静态资源拦截器,获取url路径判断访问是否为受限资源
var url = location.href;
if(url.indexOf("login.html")==-1 && url.indexOf("register.html")==-1 && url.indexOf("callback.html")==-1){
    // 你访问的是受限资源   判断你有没有登录
    var logininfo = localStorage.getItem("logininfo");
    if(!logininfo){
        // 直接跳转到登录页面
        location.href="/login.html"
    }
}

// axios前置拦截器,将携带的token请求发送给后端
axios.interceptors.request.use(result=>{
    //从浏览器中获取token
    let token = localStorage.getItem("token");
    if(token){
        //将token添加到请求头
        result.headers["token"] = token;
    }
    return result;//语法格式，必须返回
})

// axios后置拦截器
axios.interceptors.response.use(result => {
    //后端响应的是没有登录的信息
    if (!result.data.success && "noLogin" === result.data.msg) {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href = "/login.html";
    }
    return result;
})
function parseUrlParams2Obj(url) {
    let paramStr = url.substring(url.indexOf("?")+1);//code=011bLj0w3SzIXY2iME2w38TXb61bLj0Q&state=1
    let paramArr = paramStr.split("&"); //  [code=011bLj0w3SzIXY2iME2w38TXb61bLj0Q,state=1]
    let paramObj = {}; //   key:value
    for(let i = 0;i<paramArr.length;i++){
        // [code=011bLj0w3SzIXY2iME2w38TXb61bLj0Q,state=1]
        let paramTemp = paramArr[i];
        let paramName = paramTemp.split("=")[0];
        let paramValue= paramTemp.split("=")[1];
        paramObj[paramName] = paramValue;
    }
    // {code:011bLj0w3SzIXY2iME2w38TXb61bLj0Q,state:1}
    return paramObj;
}


